package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.text.TextUtils;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.GsonUtil;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.gson.reflect.TypeToken;
import com.migu.contacts.R;
import com.migu.contacts.bean.ContactBean;
import com.migu.contacts.databinding.ActivitySearchBinding;
import com.migu.contacts.databinding.ItemSearchBinding;
import com.migu.contacts.databinding.ItemSearchHistoryBinding;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SearchActivity extends BaseActivity<ActivitySearchBinding> {


    private MMKV mmkv = MMKV.defaultMMKV();

    public void onBackPressed(View view) {
        finish();
    }

    String searchHistoryKey = "search_history111";

    private List<String> getHistory() {
        String string = mmkv.getString(searchHistoryKey, "");
        if (TextUtils.isEmpty(string)) {
            return new ArrayList<>();
        } else {
            return GsonUtil.fromJson(string, new TypeToken<List<String>>() {
            }.getType());
        }
    }

    public void doSearch(View view) {
        String str = mBinding.search.getText().toString().trim();
        List<String> history = getHistory();
        if (!TextUtils.isEmpty(str)) {
            if (history.contains(str)) {
                history.remove(str);
            }
            history.add(0, str);
            if (history.size() > 10) {
                history.remove(history.size() - 1);
            }
            MMKV.defaultMMKV().putString(searchHistoryKey, GsonUtil.toJson(history));
            refreshHistory();
        }

        if (TextUtils.isEmpty(str)) {
            mBinding.resRecyclerView.setVisibility(View.GONE);
            mBinding.history.setVisibility(View.VISIBLE);
            mBinding.noResult.setVisibility(View.GONE);
        } else {
            mBinding.history.setVisibility(View.GONE);
            mBinding.resRecyclerView.setVisibility(View.VISIBLE);
            List<ContactBean> collect = all.stream().filter(new Predicate<ContactBean>() {
                @Override
                public boolean test(ContactBean bean) {
                    return bean.realName.contains(str);
                }
            }).collect(Collectors.toList());
            if (collect == null || collect.isEmpty()) {
                mBinding.noResult.setVisibility(View.VISIBLE);
            } else {
                mBinding.noResult.setVisibility(View.GONE);
            }
            resAdapter.setData(collect);
        }
    }

    @BindAdapter(R.id.recyclerView)
    private BaseAdapter<ItemSearchHistoryBinding, String> adapter = new BaseAdapter<ItemSearchHistoryBinding, String>() {
        @Override
        protected void onBindData(ItemSearchHistoryBinding binding, String bean, int position) {
            binding.tvSearchHistory.setText(bean);
            binding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mBinding.search.setText(bean);
                    doSearch(view);
                }
            });
        }
    };
    @BindAdapter(R.id.resRecyclerView)
    private BaseAdapter<ItemSearchBinding, ContactBean> resAdapter = new BaseAdapter<ItemSearchBinding, ContactBean>() {
        @OnClick(R.id.root)
        public void goDetail(ContactBean bean) {
            DataStore.put(ContactEditActivity.CONTACT_BEAN_KEY, bean);
            start(ContactEditActivity.class);
        }
    };

    @Override
    protected void initView() {
        super.initView();
        FlexboxLayoutManager layoutManager = (FlexboxLayoutManager) mBinding.recyclerView.getLayoutManager();
        layoutManager.setFlexDirection(FlexDirection.ROW);
        mBinding.resRecyclerView.setVisibility(View.GONE);
        mBinding.noResult.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        refreshHistory();
        API.contactsAll().watch(this, new HttpCallback<List<ContactBean>>() {
            @Override
            protected void onSuccess(List<ContactBean> contactBeans) {
                all = contactBeans;
            }
        });
    }

    List<ContactBean> all = new ArrayList<>();

    public void delHistory(View view) {
        mmkv.putString(searchHistoryKey, "");
        refreshHistory();
    }

    private void refreshHistory() {
        adapter.setData(getHistory());
    }

    public void create(View view) {
        DataStore.put(ContactEditActivity.CONTACT_BEAN_KEY, new ContactBean());
        start(ContactEditActivity.class);
    }
}